package cz.muni.fi.pa165.sme.client.converters;

import java.util.ArrayList;
import java.util.List;

import org.springframework.core.convert.ConversionService;

/**
 * A utility class for converting complex structures.
 *
 * @author jirankova
 */
public final class ConvertUtils {

	private ConvertUtils() {
	}

	/**
	 * Converts a list of source objects to a list of target objects using provided converter for each list item.
	 *
	 * @param sourceList source for conversion
	 * @param conversionService  conversion service; must not be null
	 * @param clazz target class
	 * @param <T>        target type
	 * @param <S>        source type
	 * @return list of target type objects
	 */
	public static <T, S> List<T> convertList(final List<S> sourceList, final ConversionService conversionService, final Class<T> clazz) {
		assert conversionService != null;

		if (sourceList == null) {
			return null;
		}

		final List<T> targetList = new ArrayList<T>(sourceList.size());

		for (S source : sourceList) {
			targetList.add(conversionService.convert(source, clazz));
		}

		return targetList;
	}
}
